<?php
/**
 * FIX ME
 * @author johan
 */
class Gallery extends xmlIO {
	private $images = array();

	/**
	 * Create the gallery object by submitting a XML-file
	 * @param unknown_type $file
	 */
	public function __construct($file){
    	parent::__construct($file);
            
        $this->loadGallery();    
	}
	
	/**
	 * Function: addComment
	 * Usage: addComment($photo, $email, $name, $IPaddress, $text, $timestamp)
	 * Adds a post entity to the xml file.
	 * $imgName is a string of the picture name, ex: "img_3327.jpg"
	 * If successful, return TRUE else FALSE.
	 * author: Andreas
	 */
	public function addComment($id, $name, $email, $timestamp, $rating, $text, $ipAddress){
		//if(isPhoto($id)){
			$comment = new Comment($id, $name, $email, $timestamp, $rating, $text, $ipAddress);
			$this->images[$id]->addComment($comment);
			
			$this->saveGallery();
			return TRUE;
		/*} else {
			return FALSE;
		}*/	
	}
	
	/**
	 * Function: addPhoto
	 * Usage: addPhoto($image, $thumbnail, $caption, $name)
	 * This function adds a photo to the xml file $filename.
	 * If successful, return TRUE else FALSE.
	 * author: Andreas
	 */
	public function addPhoto($itemID, $filename, $thumbname, $caption, $comments){
		if(!isPhoto($itemID)){
			$photo = new __construct($itemID, $filename, $thumbname, $caption, $comments);
			$this->images[$itemID] = $photo;
			
			$this->saveGallery();
			return TRUE;
		} else {
			return FALSE;
		}
	}
	
	/**
	 * Return an array with the comment objects of the current image
	 * @param unknown_type $id
	 */
	public function getCommentsById($id) {
		return $this->images[$id]->getComments();
	}
	
	/**
	 * Return an image object
	 * 
	 * @param unknown_type $id
	 * @return unknown
	 */
	public function getImageById($id) {
		return $this->images[$id];
	}
	
	/**
	 * FIX ME
	 */
	public function getImageQuantity() {
		return count($this->images);
	}
	
    /**
	 * Return an array with all images objects
	 */
	public function getImages() {
		return $this->images;
	}
	
	/**
	 * Return true if photo exists
	 * @param unknown_type $id
	 */
	public function isPhoto($id) {
		if(isset($this->images[$id])) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * FIX ME
	 */
	public function loadGallery() {
    	$root = $this->loadXML();
    	
    	$i = 0;
        
    	foreach($root as $image) {
    		$itemID = "0";
    		
            $filename = ($image->caption != null) ? $image->attributes()->image : "Error loading filename"; //required
    		$thumbname = ($image->caption != null) ? $image->attributes()->thumbnail : "Error loading thumbnail"; //required
    		$caption = ($image->caption != null) ? $image->caption : null; //not required
    		
    		// Fetch photograpgher
    		$photographer_name = ($image->photographer->name) ? $image->photographer->name : "";
    		$photographer_email = ($image->photographer->email) ? $image->photographer->email : "";
    		
            $comments = array(); //not required
    		if($image->posts != null) {
    			foreach($image->posts->children() as $comment) {
    				$commentEmail = ($comment->author->email != null) ? $comment->author->email : null;
    				$commentName = ($comment->author->name != null) ? $comment->author->name : null;
    				$commentIP = ($comment->author->IPadress != null) ? $comment->author->IPadress : null;
    				$commentText = ($comment->text != null) ? $comment->text : null;
    				$commentRating = ($comment->rating != null) ? $comment->rating : null; 
    				$commentTimestamp = ($comment->timestamp != null) ? $comment->timestamp : null;
    				
    				$comments[] = new Comment($itemID, $commentName, $commentEmail, $commentTimestamp, $commentRating, $commentText, $commentIP);
    			}
    		}
    		
    		$this->images[] = new Image($itemID, $photographer_name, $photographer_email, $filename, $thumbname, $caption, $comments);
    		$i++;
        }
	}
	
	/**
	 * Saves the gallery to a xml file.
	 * author: Andreas
	 */
	public function saveGallery() {
		$xml = new SimpleXMLElement('<!DOCTYPE gallery SYSTEM "gallery.dtd"><gallery xmlns="http://www.gidenstam.org/2010/PhotoGallery"></gallery>');
		//add photos
		foreach($this->images as $image){
			$photo = $xml->addChild('photo');
			$photo->addAttribute('image', $image->getThumbname());
			$photo->addAttribute('thumbnail', $image->getThumbname());
			$caption = $photo->addChild('caption', $image->getCaption());
			$photographer = $photo->addChild('photographer');
			$photographer_name = $photographer->addChild('name', $image->getAuthorName());
			$photographer_email = $photographer->addChild('email', $image->getAuthorEmail());
			$posts = $photo->addChild('posts');
			
			//add comments
			if($image->getComments() != null) {
				foreach($image->getComments() as $comment){
					$post = $posts->addChild('post');
					$author = $post->addChild('author');
					$email = $author->addChild('email', $comment->getEmail());
					$name = $author->addChild('name', $comment->getName());
					$IPaddress = $author->addChild('IPaddress', $comment->getipAddress());
					$text = $post->addChild('text', $comment->getText());
					$rating = $post->addChild('rating', $comment->getRating());
					$timestamp = $post->addChild('timestamp', $comment->getTimestamp());
				}
			}
		}
		
		$this->saveXML($xml);
	}
}